<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use think\Exception;
use AstroTool\Cache\CacheManage;

class ZlCenterAdvertVersion extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_center_advert_version';

    protected $cacheModel = 'active.center_advert';

    /**
     * 获取个人中心广告信息
     * @param int $advertIndex 1=>开通 2=>续费 3=>激活
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function getPersonalAdvertInfo(int $advertIndex)
    {
        $headers = request()->header();

        $version = $headers['version'] ?? '';
        $type = strtolower($headers['app-type'] ?? '');

        $advertInfo = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type . ':' . $version)->getDataAndSet(function () use ($type, $version)
        {
            $data = $this->getAdvertInfoInData($type, $version);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type . ':' . $version);
            return $data;
        });

        $advertInfo = json_decode($advertInfo, true);

        $data = [];
        foreach ($advertInfo as $item)
        {
            if (in_array($item['index'], [1, 2, 3], true))
            {
                if ($item['index'] === $advertIndex)
                {
                    $item['index'] = 1;
                    $data[] = $item;
                }
            } else
            {
                if ($item['index'] === 4)
                {
                    $item['index'] = 2;
                } else if ($item['index'] === 5)
                {
                    $item['index'] = 3;
                }
                $data[] = $item;
            }
        }

        return $data;
    }

    /**
     * 通过数据库获取个人中心广告信息
     * @param string $type
     * @param string $version
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \Exception
     */
    public function getAdvertInfoInData(string $type, string $version)
    {
        try
        {
            // 获取版本信息
            $versionId = (new AppVersion())->getVersionInfo($type, $version, ['id']);
            $versionId = is_array($versionId) ? current($versionId) : $versionId;

            $advertInfo = $this->alias('cv')
                ->join('wechat_backup_07_18_14_43_00.zl_center_advert ca', 'ca.id=cv.ca_id')
                ->where('cv.version_id', $versionId)
                ->where('ca.status', 1)
                ->field('ca.image,ca.target,ca.link,ca.position')
                ->group('ca.position')
                ->select();

            $data = [];
            foreach ($advertInfo as $v)
            {
                $data[] = [
                    'img' => $v['image'],
                    'target' => $v['target'],
                    'link' => $v['link'],
                    'index' => (int)$v['position'],
                ];
            }

            $advertInfo = json_encode($data);
        }
        catch (Exception $e)
        {
            return '';
        }

        return $advertInfo;
    }
}
